<?php

/**
 * 短信接口
 */
class MessagesController extends ControllerBase
{

    protected $mes_request_url;//短信请求地址

    public function initialize()
    {
        parent::initialize();

        $this->mes_request_url = [
            'utf8' => 'http://sms.chanzor.com:8001/sms.aspx',
            'GB2312' => 'http://sms.chanzor.com:8001/smsGBK.aspx',
        ];
    }

    function get_user($ch, $apikey)
    {
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v1/user/get.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
        return curl_exec($ch);
    }

    function send($ch, $data)
    {
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v1/sms/send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }

    function tpl_send($ch, $data)
    {
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v1/sms/tpl_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }

    function voice_send($ch, $data)
    {
        curl_setopt($ch, CURLOPT_URL, 'http://voice.yunpian.com/v1/voice/send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }

    public function indexAction()
    {

        header("Content-Type:text/html;charset=utf-8");
        $apikey = "1342b1e2542479d1ac1355a605931429"; //修改为您的apikey(https://www.yunpian.com)登陆官网后获取
        $mobile = "13980073024"; //请用自己的手机号代替
        $text = "【云片网】您的验证码是1234";
        $ch = curl_init();

        /* 设置验证方式 */

        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));

        /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        /* 设置超时时间*/
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 取得用户信息
        $json_data = $this->get_user($ch, $apikey);
        $array = json_decode($json_data, true);
        echo '<pre>';
        print_r($array);
        // 发送短信
        //$data = array('text' => $text, 'apikey' => $apikey, 'mobile' => $mobile);
        //$json_data = send($ch, $data);
        //$array = json_decode($json_data, true);
        //echo '<pre>';
        //print_r($array);
        // 发送模板短信
        // 需要对value进行编码
        //$data = array('tpl_id' => '1', 'tpl_value' => ('#code#') . '=' . urlencode('1234') . '&' . urlencode('#company#') . '=' . urlencode('欢乐行'), 'apikey' => $apikey, 'mobile' => $mobile);
        //print_r($data);
        //$json_data = tpl_send($ch, $data);
        //$array = json_decode($json_data, true);
        //echo '<pre>';
        //print_r($array);

        // 发送语音验证码
        //$data = array('code' => '9876', 'apikey' => $apikey, 'mobile' => $mobile);
        //$json_data = voice_send($ch, $data);
        //$array = json_decode($json_data, true);
        //echo '<pre>';
        //print_r($array);

        curl_close($ch);

        /***************************************************************************************/
        //获得账户


        exit();

    }

    /**
     * 发送
     */
    public function sendAction()
    {
        $this->view->pick('messages/send');
    }

}